﻿Imports Do_An.TinTucTableAdapters
Imports System.Data
Imports System.Data.SqlClient

Public Class WebForm1
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim maLoai As String = Request.QueryString("MaLoai").ToString()
        PhanTrang(maLoai)
        If IsPostBack = False Then
            BindData(1, maLoai)
            'Dim tintucAdapters As New TinTucTableAdapters.BaiBaoTableAdapter
            'DLTinTucTheoChuDe.DataSource = tintucAdapters.GetBaiBaobyMaLoai(maLoai)
            'DLTinTucTheoChuDe.DataBind()
        Else
            If Request.QueryString("page") <> vbNull And Request.QueryString("page").ToString() <> String.Empty Then
                BindData(Integer.Parse(Request.QueryString("page")), maLoai)
            End If
        End If
    End Sub

    Const sobanghi As Integer = 2
    Function BindToDataTable(ByVal index As Integer, ByVal sql As String) As DataTable
        Dim da As SqlDataAdapter = New SqlDataAdapter(sql, ConfigurationManager.ConnectionStrings("TinTucConnectionString").ConnectionString)
        Dim dt As DataTable = New DataTable()
        da.Fill(sobanghi * (index - 1), sobanghi, dt)
        Return dt
    End Function

    Sub PhanTrang(ByVal id As String)
        Dim dt As New TinTuc.BaiBaoDataTable()
        Dim tintucAdapter As New TinTucTableAdapters.BaiBaoTableAdapter
        tintucAdapter.Fill(dt, id)
        Dim t As Integer = dt.Rows.Count / sobanghi
        Dim sotrang As New Integer
        If (t * sobanghi < dt.Rows.Count) Then
            sotrang = t + 1
        Else
            sotrang = t
        End If
        Dim link As String = String.Empty
        Dim i As New Integer
        For i = 0 To i < sotrang
            link += "<a href='TinTheoTheLoai.aspx?page=" + i + "'>" + i + "</a>"
            i = i + 1
        Next
        litPages.Text = "Trang " + link
    End Sub

    Sub BindData(ByVal index As Integer, ByVal id As String)
        Dim dt As DataTable = New DataTable()
        dt = BindToDataTable(index, "Select * from BaiBao where MaLoai=" + id)
        DLTinTucTheoChuDe.DataSource = dt
        DLTinTucTheoChuDe.DataBind()
    End Sub
End Class